﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace FastData.Mapping
{
    internal class DataTypeInfo
    {
        private static ConcurrentDictionary<Type, DataTypeInfo> _dataTypeInfo = new ConcurrentDictionary<Type, DataTypeInfo>();

        internal static DataTypeInfo GetInfo(Type type)
        {
            DataTypeInfo info;

            if (!_dataTypeInfo.TryGetValue(type, out info))
            {
                info = new DataTypeInfo(type);
                _dataTypeInfo.TryAdd(type, info);
            }

            return info;
        }

        internal readonly List<DataPropertyInfo> Properties = new List<DataPropertyInfo>();
        internal readonly string[] PropertyNames;

        internal DataTypeInfo(Type type)
        {
            var properties = type.GetDataProperties();
            foreach (var item in properties)
            {
                Properties.Add(new DataPropertyInfo(item));
            }

            PropertyNames = Properties.Select(prop => prop.Name).ToArray();
        }

    }
}
